package statistics.dna;

import java.util.ArrayList;

import statistics.Collectible;
import statistics.Options;
import dnaModels.DNASequence;

/**
 * A statistic that tracks the number of haplotypes observed in a sample of individuals
 * @author brendan
 *
 */
public class NumHaplotypes extends DNAStatistic {

	public static final String identifier = "Number of Haplotypes";
	
	ArrayList<DNASequence> haps;
//	private int regionStart = 0;
//	private int regionEnd = 300;
	
	public NumHaplotypes() {
		values = new ArrayList<Double>();
		haps = new ArrayList<DNASequence>();
	}
	
	public NumHaplotypes getNew(Options ops) {
		this.options = ops;
		return new NumHaplotypes();
	}

	
	public void collect(Collectible pop) {
		haps.clear();
		for(int i=0; i<pop.size(); i++) {
			int index = hapsContainsSequence(pop.getInd(i).getPrimaryDNA());
			if (index<0) {
				haps.add(pop.getInd(i).getPrimaryDNA());
			}
		}
		
		values.add((double)haps.size());
	}

	public boolean showOnScreenLog() {
		return true;
	}
	
	private int hapsContainsSequence(DNASequence seq) {
		for(int i=0; i<haps.size(); i++) {
			if (regionMatches(haps.get(i), seq, 0, seq.length()))
				return i;
		}
		return -1;
	}
	
	private static boolean regionMatches(DNASequence a, DNASequence b, int start, int end) {
		for(int site = start; site<end; site++) {
			if (a.getBaseChar(site)!=b.getBaseChar(site)) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	public String getDescription() {
		return "Number of haplotypes in sample";
	}

	public String getIdentifier() {
		return identifier;
	}
	
}
